package com.mmm.nlp.impl;

import com.mmm.nlp.LexiconMatcher;
import com.mmm.nlp.Token;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * User: Zhendong
 * Date: 4/20/13
 * Time: 11:40 AM
 * To change this template use File | Settings | File Templates.
 */
public class HashLexiconMatcher implements LexiconMatcher
{
  private HashMap<String, String> lexicon_dictionary;

  public HashLexiconMatcher(String lexiconFilePath)
  {
    lexicon_dictionary = new HashMap<String, String>();
    try
    {
      BufferedReader br = new BufferedReader(new FileReader(lexiconFilePath));
      String line = null;
      while ((line = br.readLine())!=null)
      {
        String[] arr = line.split("\t");
        lexicon_dictionary.put(arr[0], arr[1]);
      }
      br.close();
    }catch (Exception ex)
    {
      System.out.println("Failed to load lexicon dictionary resources.");
      ex.printStackTrace();
      System.exit(-1);
    }
  }

  @Override
  public void match(Token token)
  {
    if(token.isWord())
    {
      String result = lexicon_dictionary.get(token.getBody().toUpperCase());
      if(result != null)
      {
        token.setLexicon(result);
      }
    }
  }
}
